//
//  ViewController.m
//  AStart
//
//  Created by apple on 2019/5/15.
//  Copyright © 2019 apple. All rights reserved.
//

#import "ViewController.h"
#include "astar.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    for (int x = 0; x < 15; x++) {
        
        NSMutableArray *viewArray = [[NSMutableArray alloc] init];
        
        for (int y = 0; y < 15; y++) {
            NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(x*30, y*30, 30, 30)];
            
            view.wantsLayer = true; // 设置背景颜色
            [view setNeedsDisplay:YES];
            
            [self.view addSubview:view];
            [viewArray addObject:view];
        }
        [array addObject:viewArray];
    }

    // 地图数据
    char maps[15][15] =
    {
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }
    };
    
    // 搜索参数
    AStar::Params param;
    param.width     = 15;                                   // 地图宽度
    param.height    = 15;                                   // 地图高度
    param.corner    = 0;                                    // 寻路规则
    param.start     = AStar::Vec2(6, 10);                   // 开始点
    param.end       = AStar::Vec2(14, 6);                  // 目标点
    param.can_pass  = [&](const AStar::Vec2 &pos)->bool     // 是否可以通过回调函数
    {
        return maps[pos.x][pos.y] == 0;
    };
    
    // 执行搜索
    AStar algorithm;
    auto path = algorithm.find(param);
    
    for (int x = 0; x < 15; x++) {
        for (int y = 0; y < 15; y++) {
            NSView *view = array[x][y];
            
            view.wantsLayer = true; // 设置背景颜色
            [view setNeedsDisplay:YES];
            
            if (maps[x][y] == 0) {
                view.layer.backgroundColor = [NSColor gridColor].CGColor;
            }else{
                view.layer.backgroundColor = [NSColor yellowColor].CGColor;
            }
        }
    }
    
    for(auto& coordinate : path) {
        NSView *view = array[coordinate.x][coordinate.y];
        view.layer.backgroundColor = [NSColor greenColor].CGColor;
    }
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

@end
